/* 解决层层嵌套的异步回调的问题， 回调地狱 */
// new Promise()
// .then(() => {}, () => {})
// .then(() => {}, () => {})

const BASEURL = 'https://www.openrzcl.com/prod-out-api';
export default (params) => {
    return new Promise((resolve, reject) => {
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        const header = {
            'Content-type': "application/json",
            'Authorization': wx.getStorageSync('token')
        }
        let token = wx.getStorageSync('token')
        token && (header.token = token)
        // 1. promise的状态初始化 pending
        // 执行异步任务
        wx.request({
            url: BASEURL + params.url,
            data: params.data || {},
            method: params.method || 'GET',
            header,
            success: (res) => {
                // 2.1 修改promise的状态为成功状态  resolved
                resolve(res.data);
            },
            fail: (err) => {
                console.log(err);
                // 2.2 修改promise的状态为失败状态 rejected
                reject(err);
            },
            complete: () => {
                wx.hideLoading()
            }
        })
    })
}

// let fn = 封装发送请求的基本功能函数
// let result = await fn()
